package com.qnw.CardGroupManagement;

import android.app.*;
import android.database.*;
import android.database.sqlite.*;
import android.os.*;
import android.text.*;
import android.view.*;
import android.view.View.*;
import android.widget.*;
import java.io.*;
import java.util.*;
import com.ygo.feihua.R;
import com.ygo.feihua.Gongju;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import com.qnw.CardGroupManagement.management.KzlistManagement;
import android.widget.AdapterView.OnItemClickListener;
import com.ygo.feihua.util.*;
import com.qnw.CardGroupManagement.bean.*;

public class E5  extends AppCompatActivity implements OnClickListener
{
	private EditText ed_kzm;
	private Button bu_nt;
	private Button bu_zk;
	private EditText tx_kb;
	KzlistManagement kzm;
	private Gongju gj;
	private int bj=0;
	FileWriter fw;
	private String sjkpath;
	private DialogUtils du;

	public void onClick(View p1)
	{
		switch (p1.getId())
		{
			case R.id.e5_bu_nt:
				{
					bj = 1;
					ClipboardManager 名字= (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
					tx_kb.setText(名字.getText().toString().trim());
					break;
				}
			case R.id.e5_bu_zk:
				final List<DeckType> da=kzm.getType();
				String ss[]=new String[da.size()-1];
				for(int i=0;i<ss.length;i++){
					ss[i]=da.get(i+1).getName();
				}
				
				ListView ll=(ListView) du.dialogl(true,"保存到",ss);
				ll.setOnItemClickListener(new OnItemClickListener(){

						@Override
						public void onItemClick(AdapterView<?> p1, View p2, final int p3, long p4)
						{
							
							if (TextUtils.isEmpty(ed_kzm.getText().toString()))
							{
								gj.show( "不能为空");
								return; 
							}
							if (new File(da.get(p3+1).getPath() + ed_kzm.getText().toString() + ".ydk").exists())
							{
								gj.show("卡组名已存在");
								return; 
							}
						 
							 final ArrayList<String> zkzs=new ArrayList<String>();
							final ArrayList<String> ews=new ArrayList<String>();
							final ArrayList<String> fkzs=new ArrayList<String>();
							if (bj == 1)
							{
								du.dialogj1(false,"","生成卡组中,请稍等");
								new Thread(new Runnable(){

										@Override
										public void run()
										{
											


											try
											{
												int bj2=0;
												fw = new FileWriter(da.get(p3+1).getPath()+ ed_kzm.getText().toString() + ".ydk");
												SQLiteDatabase sjk=SQLiteDatabase.openOrCreateDatabase(sjkpath, null);
												Cursor datas,texts;
												String[] ss=tx_kb.getText().toString().trim().split("\n");
												for (int i=0;i < ss.length;i++)
												{
													if (ss[i].contains("主卡组"))
													{
														bj2 = 1;									
														continue;
													}
													if (ss[i].contains("额外"))
													{
														bj2 = 2;									
														continue;
													}
													if (ss[i].contains("副卡组"))
													{
														bj2 = 3;									
														continue;
													}

													switch (bj2)
													{
														case 0:
															{
																break;
															}
														case 1:
															{
																texts = sjk.query("texts", new String[]{"id","name"}, "name = ?", new String[]{ss[i]}, null, null, null);
																if (texts.getCount() == 0)
																	continue;
																texts.moveToNext();
																datas = sjk.query("datas", new String[]{"id","type"}, "id = ?", new String[]{texts.getString(0)}, null, null, null);
																if (datas.getCount() == 0)
																	continue;
																datas.moveToNext();
																if (!lx(datas.getString(1)).contains("超量") && !lx(datas.getString(1)).contains("融合") && !lx(datas.getString(1)).contains("同调"))
																{
																	zkzs.add(texts.getString(0));
																}
																texts.close();
																datas.close();
																break;
															}
														case 2:
															{
																texts = sjk.query("texts", new String[]{"id","name"}, "name = ?", new String[]{ss[i]}, null, null, null);
																if (texts.getCount() == 0)
																	continue;
																texts.moveToNext();
																datas = sjk.query("datas", new String[]{"id","type"}, "id = ?", new String[]{texts.getString(0)}, null, null, null);
																if (datas.getCount() == 0)
																	continue;
																datas.moveToNext();
																if (lx(datas.getString(1)).contains("超量") || lx(datas.getString(1)).contains("融合") || lx(datas.getString(1)).contains("同调"))
																{
																	ews.add(texts.getString(0));
																}
																texts.close();
																datas.close();
																break;
															}
														case 3:
															{
																texts = sjk.query("texts", new String[]{"id","name"}, "name = ?", new String[]{ss[i]}, null, null, null);
																if (texts.getCount() == 0)
																	continue;
																texts.moveToNext();
																fkzs.add(texts.getString(0));
																texts.close();
																break;
															}					
													}
												}
												fw.write("#created by ...\n#main");
												for(int i=0;i<60&&i<zkzs.size();i++)
												{
													fw.write("\n"+zkzs.get(i));
												}
												fw.write("\n#extra");
												for(int i=0;i<15&&i<ews.size();i++)
												{
													fw.write("\n"+ews.get(i));
												}
												fw.write("\n!side");
												for(int i=0;i<15&&i<fkzs.size();i++)
												{
													fw.write("\n"+fkzs.get(i));
												}
												fw.close();
												Message me=new Message();
												me.what=0;
												me.obj="已保存到"+da.get(p3+1).getPath() + ed_kzm.getText().toString() + ".ydk";
												
												handle.sendMessage(me);
											}
											catch (Exception e)
											{
												Message me=new Message();
												me.what=0;
												me.obj="组卡失败,原因为"+e;

												handle.sendMessage(me);
												
											}
											
											
											// TODO: Implement this method
										}
									}).start();
								
							}
							else
							{
								gj.show("卡表不能为空");
							}
							
							// TODO: Implement this method
						}
					});
				
			
					break;
				
		}
	}
	
	Handler handle=new Handler(){

		@Override
		public void handleMessage(Message msg)
		{
			// TODO: Implement this method
			super.handleMessage(msg);
			switch(msg.what){
				case 0:
					du.dis();
					gj.show(msg.obj+"");
					break;
				
			}
		}
	};
	
	
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		//requestWindowFeature(Window.FEATURE_NO_TITLE);
		this.setContentView(R.layout.e5_layout);

		Toolbar tool=(Toolbar) findViewById(R.id.toolbar);
		CgmaZhuye.getType();
		gj=Gongju.getdx(this);
		du=DialogUtils.getdx(this);
		gj.cshToolbar(tool,"文字组卡");
		sjkpath=SharedPreferenceUtil.getCdbpath();
		kzm=KzlistManagement.getdx();
		
		
		ed_kzm = (EditText)findViewById(R.id.e5_ed_kzm);
		bu_nt = (Button)findViewById(R.id.e5_bu_nt);
		bu_zk = (Button)findViewById(R.id.e5_bu_zk);
		tx_kb = (EditText)findViewById(R.id.e5_tx_kb);

		bu_nt.setOnClickListener(this);
		bu_zk.setOnClickListener(this);
	}




	public static String lx(String lx)
	{
		String lx4="";
		String[] lx1={"1","2","4","10","20","40","80","200","400","800","1000","2000","4000","10000","20000","40000","80000","100000","200000","400000","800000","1000000"};
		String[] lx3={"怪兽","魔法","陷阱","通常","效果","融合","仪式","灵魂","同盟","二重","调整","同调","衍生物","速攻","永续","装备","场地","反击","反转","卡通","超量","灵摆"};
		for (int i=0;i < lx1.length;i++)
		{
			int lx2=Integer.parseInt(lx) & Integer.valueOf(lx1[i], 16);//把获得的type与各个类型做与运算得出有哪些类型,如果有则等于那个值,没有则为0
			if (lx2 == Integer.valueOf(lx1[i], 16))
			{
				lx4 = lx4 + "|" + lx3[i];
			}
		}
		return lx4;
	}
}
